﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using StoryPlan.Models;

namespace StoryPlan.Api
{
    public class CharacterController : ApiControllerBase
    {
        private MainDataContext db = new MainDataContext();

        // GET api/Character
        public IEnumerable<Character> GetCharacters(int storyId = -1)
        {
            var id = GetUserId();

            if (storyId == -1)
                return db.Characters.Where(c => c.UserId == id).AsEnumerable();
            else
                return db.Characters.Where(c => c.UserId == id && c.Stories.Any(s => s.Id == storyId));
        }

        // GET api/Character/5
        public Character GetCharacter(int id)
        {
            Character character = db.Characters.Find(id);
            if (character == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return character;
        }

        // PUT api/Character/5
        public HttpResponseMessage PutCharacter(int id, Character character)
        {
            if (ModelState.IsValid && id == character.Id)
            {
                db.Entry(character).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Character
        public HttpResponseMessage PostCharacter(Character character)
        {
            if (ModelState.IsValid)
            {
                db.Characters.Add(character);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, character);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = character.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Character/5
        public HttpResponseMessage DeleteCharacter(int id)
        {
            Character character = db.Characters.Find(id);
            if (character == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Characters.Remove(character);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, character);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}